package com.example.controller;

import com.example.service.PoetryService;
import jakarta.annotation.Resource;
import org.seimicrawler.xpath.core.node.Num;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.html.HTMLImageElement;

import java.util.*;

@RestController
@CrossOrigin
public class LocationController {
    @Resource
    private PoetryService poetryService;

    @PostMapping("/location/{locate_id}/{flag}")//某地区的诗人列表  //若返回全部的 后续用Flag区分
    public List<HashMap<String,Object>> location_poetry(@PathVariable int locate_id,@PathVariable int flag,@RequestBody Map<String,Object> req) {//url中接收地区代码
        List ListOfHash =new ArrayList();
        if(poetryService.locate_and_poetry(locate_id).size()!=0||(poetryService.locate_all_poetry(locate_id).size()!=0)){
            int num=poetryService.locate_and_poetry(locate_id).size();
            int start=0,end=0;
            if(flag==1){//抽屉内地区展示
                if(num>=20)num=20;
                end=num-1;
            }
            else{ //诗人库界面 地区诗人分页展示
                if((Number)req.get("page")!=null&&(Number)req.get("perPage")!=null){
                    int page=((Number)req.get("page")).intValue();
                    int perPage=((Number)req.get("perPage")).intValue();
                    start=page*perPage;
                    end=(page+1)*perPage-1;
                }
            }
            poetryService.alter_code();//将text字段类型改为int 以减小存储空间和数据查询速度
            //long startTime = System.currentTimeMillis(); //程序开始记录时间
            if(flag==0&&String.valueOf(locate_id).length()==2){
                for(int i=start;i<=end;i++){
                    HashMap<String,Object> hashmap=new HashMap<>();
                    int len=poetryService.get_locate_poetry(String.valueOf(locate_id).concat("0000"));
                    if(i<len){
                        hashmap.put("id",poetryService.locate_all_poetry(locate_id).get(i).getId()); //id作为主键 后期都以id来检索数据库！
                        hashmap.put("name",poetryService.locate_all_poetry(locate_id).get(i).getPoetry_name());
                        hashmap.put("image",poetryService.locate_all_poetry(locate_id).get(i).getImage());
                        String summary=poetryService.locate_all_poetry(locate_id).get(i).getSummary();
                        int index=summary.indexOf("►");
                        hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
                        ListOfHash.add(hashmap);
                    }
                    else{
                        break;
                    }
                }
            }
            else{
                for(int i=start;i<=end;i++){
                    HashMap<String,Object> hashmap=new HashMap<>();

                    //添加索引：26218
//                poetryService.drop_index();
//                poetryService.create_index();

                    //视图法：11120  12055
//                poetryService.drop_view();
//                poetryService.create_view(locate_id);
//                poetryService.select_view(locate_id);
                    int len=0;
                    //普通查询：11344 9619(int)
                    if(locate_id==0){
                        len=poetryService.locate_and_poetry(0).size();
                    }
                    else {
                        len=poetryService.get_locate_poetry(String.valueOf(locate_id));
                    }
                    if(i<len){
                        hashmap.put("id",poetryService.locate_and_poetry(locate_id).get(i).getId()); //id作为主键 后期都以id来检索数据库！
                        hashmap.put("name",poetryService.locate_and_poetry(locate_id).get(i).getPoetry_name());
                        //System.out.println("根据地区："+poetryService.locate_and_poetry(locate_id).get(i).getPoetry_name());
                        hashmap.put("image",poetryService.locate_and_poetry(locate_id).get(i).getImage());
                        String summary=poetryService.locate_and_poetry(locate_id).get(i).getSummary();
                        int index=summary.indexOf("►");
                        hashmap.put("summary",summary.substring(0,index));//去除三角形后的部分
                        ListOfHash.add(hashmap);
                    }
                    else{
                        break;
                    }
                }
            }

            //long endTime   = System.currentTimeMillis(); //程序结束记录时间
            //long TotalTime = endTime - startTime;       //总消耗时间
            //System.out.println(TotalTime);
        }
        //else ListOfHash.add("");
        return ListOfHash;
    }

    @GetMapping("/location/{locate_id}/{perPage}")//根据地区分页数量
    public HashMap<String,Object> locate_pagenum(@PathVariable int locate_id,@PathVariable int perPage) {
        HashMap<String,Object> hashmap=new HashMap<>();
        if(String.valueOf(locate_id).length()==2){
            locate_id*=10000;
        }
        int total=0;
        if(locate_id==0){
            total=poetryService.locate_and_poetry(0).size();
        }
        else{
            total=poetryService.get_locate_poetry(String.valueOf(locate_id));
        }
        int page_num=total/perPage;
        if(total%perPage!=0){
            page_num++;
        }
        hashmap.put("page_num",page_num);
        return hashmap;
    }

    @GetMapping("/location/{locate_id}")//地区诗人总数
    public HashMap<String,Object> locate_poetrynum(@PathVariable int locate_id) {
        HashMap<String,Object> hashmap=new HashMap<>();
        if(locate_id==0){
            hashmap.put("poetry_num",poetryService.locate_and_poetry(0).size());
        }
        else{
            hashmap.put("poetry_num",poetryService.get_locate_poetry(String.valueOf(locate_id)));
        }
        return hashmap;
    }
}
